<?php

namespace app\common\model\distribute;

class HandleVideoAuthCommission
{
    /**
     * Info: 直推红娘
     * Date: 2023/1/29 16:36
     */
    public static function updateDirectUserCommission (
        $memberModel,
        $userModel,
        $balanceRecordsModel,
        $direct_user_id,
        $order_id,
        $zt_commission_money,
        $isAdd = true)
    {
        $findUserRes = $memberModel->findMember(['user_id' => $direct_user_id]);
        if ($findUserRes['errorCode'] == 0) {
            if ($isAdd) {
                $data = [
                    'total_balance' => (float)$zt_commission_money + (float)$findUserRes['data']['total_balance'],
                    'balance' => (float)$zt_commission_money + (float)$findUserRes['data']['balance'],
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$zt_commission_money,
                ];
                $zj_state = 0;
            } else {
                $data = [
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$zt_commission_money,
                ];
                $zj_state = 2;
            }
            $memberModel->updateMember(['user_id' => $direct_user_id], $data);
            echo $memberModel->getLastSql()."\r\n";
            $userModel->updateUser(['id' => $direct_user_id], $data);
            echo $userModel->getLastSql()."\r\n";
            $balanceRecordsModel->updateCommissionRecords([
                'user_id' => $direct_user_id,
                'order_id' => $order_id,
                'zj_state' => 1
            ],['zj_state' => $zj_state]);
            echo $balanceRecordsModel->getLastSql()."\r\n";
            echo '----------updateDirectUserCommission-----------'."\r\n";
        }
    }

    /**
     * Info: 间推红娘
     * Date: 2023/1/29 16:36
     */
    public static function updateIndirectUserCommission (
        $memberModel,
        $userModel,
        $balanceRecordsModel,
        $indirect_user_id,
        $order_id,
        $jt_commission_money,
        $isAdd = true)
    {
        $findUserRes = $memberModel->findMember(['user_id' => $indirect_user_id]);
        if ($findUserRes['errorCode'] == 0) {
            if ($isAdd) {
                $data = [
                    'total_balance' => (float)$jt_commission_money + (float)$findUserRes['data']['total_balance'],
                    'balance' => (float)$jt_commission_money + (float)$findUserRes['data']['balance'],
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$jt_commission_money,
                ];
                $zj_state = 0;
            } else {
                $data = [
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$jt_commission_money,
                ];
                $zj_state = 2;
            }
            $memberModel->updateMember(['user_id' => $indirect_user_id], $data);
            echo $memberModel->getLastSql()."\r\n";
            $userModel->updateUser(['id' => $indirect_user_id], $data);
            echo $userModel->getLastSql()."\r\n";
            $balanceRecordsModel->updateCommissionRecords([
                'user_id' => $indirect_user_id,
                'order_id' => $order_id,
                'zj_state' => 1
            ],['zj_state' => $zj_state]);
            echo $balanceRecordsModel->getLastSql()."\r\n";
            echo '----------updateIndirectUserCommission-----------'."\r\n";
        }
    }

    /**
     * Info: 市代用户
     * Date: 2023/1/29 16:36
     */
    public static function updateShiAdminCommission (
        $adminModel,
        $adminBalanceRecordsModel,
        $shi_admin_id,
        $order_id,
        $shi_commission_money,
        $isAdd = true)
    {
        $findUserRes = $adminModel->findAdmin(['id' => $shi_admin_id]);
        if ($findUserRes['errorCode'] == 0) {
            if ($isAdd) {
                $data = [
                    'total_balance' => (float)$shi_commission_money + (float)$findUserRes['data']['total_balance'],
                    'balance' => (float)$shi_commission_money + (float)$findUserRes['data']['balance'],
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$shi_commission_money,
                ];
                $zj_state = 0;
            } else {
                $data = [
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$shi_commission_money,
                ];
                $zj_state = 2;
            }
            $adminModel->updateAdmin(['id' => $shi_admin_id], $data);
            echo $adminModel->getLastSql()."\r\n";
            $adminBalanceRecordsModel->updateCommissionRecords([
                'user_id' => $shi_admin_id,
                'order_id' => $order_id,
                'zj_state' => 1
            ],['zj_state' => $zj_state]);
            echo $adminBalanceRecordsModel->getLastSql()."\r\n";
            echo '----------updateShiAdminCommission-----------'."\r\n";
        }
    }

    /**
     * Info: 区县代用户
     * Date: 2023/1/29 16:37
     */
    public static function updateXianAdminCommission (
        $adminModel,
        $adminBalanceRecordsModel,
        $xian_admin_id,
        $order_id,
        $xian_commission_money,
        $isAdd = true)
    {
        $findUserRes = $adminModel->findAdmin(['id' => $xian_admin_id]);
        if ($findUserRes['errorCode'] == 0) {
            if ($isAdd) {
                $data = [
                    'total_balance' => (float)$xian_commission_money + (float)$findUserRes['data']['total_balance'],
                    'balance' => (float)$xian_commission_money + (float)$findUserRes['data']['balance'],
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$xian_commission_money,
                ];
                $zj_state = 0;
            } else {
                $data = [
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$xian_commission_money,
                ];
                $zj_state = 2;
            }
            $adminModel->updateAdmin(['id' => $xian_admin_id], $data);
            echo $adminModel->getLastSql()."\r\n";
            $adminBalanceRecordsModel->updateCommissionRecords([
                'user_id' => $xian_admin_id,
                'order_id' => $order_id,
                'zj_state' => 1
            ],['zj_state' => $zj_state]);
            echo $adminBalanceRecordsModel->getLastSql()."\r\n";
            echo '----------updateXianAdminCommission-----------'."\r\n";
        }
    }

    /**
     * Info: 客服部
     * Date: 2023/1/29 16:37
     */
    public static function updateKfAdminCommission (
        $adminModel,
        $adminBalanceRecordsModel,
        $kf_admin_id,
        $order_id,
        $kf_commission_money,
        $isAdd = true)
    {
        $findUserRes = $adminModel->findAdmin(['id' => $kf_admin_id]);
        if ($findUserRes['errorCode'] == 0) {
            if ($isAdd) {
                $data = [
                    'total_balance' => (float)$kf_commission_money + (float)$findUserRes['data']['total_balance'],
                    'balance' => (float)$kf_commission_money + (float)$findUserRes['data']['balance'],
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$kf_commission_money,
                ];
                $zj_state = 0;
            } else {
                $data = [
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$kf_commission_money,
                ];
                $zj_state = 2;
            }
            $adminModel->updateAdmin(['id' => $kf_admin_id], $data);
            echo $adminModel->getLastSql()."\r\n";
            $adminBalanceRecordsModel->updateCommissionRecords([
                'user_id' => $kf_admin_id,
                'order_id' => $order_id,
                'zj_state' => 1
            ],['zj_state' => $zj_state]);
            echo $adminBalanceRecordsModel->getLastSql()."\r\n";
            echo '----------updateKfAdminCommission-----------'."\r\n";
            // 给客服部各员工分润
            $kfbStaffListRes = $adminModel->findAllAdmin(['bid' => $kf_admin_id]);
            if ($kfbStaffListRes['errorCode'] == 0) {
                $kfAdminMoney = $kf_commission_money;
                array_map(function ($staff) use ($kfAdminMoney,$adminModel,$adminBalanceRecordsModel,$isAdd,$order_id) {
                    if (floatval($staff['rate'])) {
                        $staff_commission_money = $staff['rate'] * $kfAdminMoney;
                        $staff_commission_money = (float)sprintf("%.2f", substr(sprintf("%.3f", $staff_commission_money), 0, -1));
                        if ($isAdd) {
                            $data = [
                                'total_balance' => $staff_commission_money + (float)$staff['total_balance'],
                                'balance' => $staff_commission_money + (float)$staff['balance'],
                                'commission' => (float)$staff['commission'] - $staff_commission_money,
                            ];
                            $zj_state = 0;
                        } else {
                            $data = [
                                'commission' => (float)$staff['commission'] - $staff_commission_money,
                            ];
                            $zj_state = 2;
                        }
                        $adminModel->updateAdmin(['id' => $staff['id']], $data);
                        echo $adminModel->getLastSql()."\r\n";
                        $adminBalanceRecordsModel->updateCommissionRecords([
                            'user_id' => $staff['id'],
                            'order_id' => $order_id,
                            'zj_state' => 1
                        ],['zj_state' => $zj_state]);
                        echo $adminBalanceRecordsModel->getLastSql()."\r\n";
                        echo '----------员工-----------'."\r\n";
                    }
                }, $kfbStaffListRes['data']);
            }
        }
    }

    /**
     * Info: 培训部
     * Date: 2023/1/29 16:38
     */
    public static function updatePxAdminCommission (
        $adminModel,
        $adminBalanceRecordsModel,
        $px_admin_id,
        $order_id,
        $px_commission_money,
        $isAdd = true)
    {
        $findUserRes = $adminModel->findAdmin(['id' => $px_admin_id]);
        if ($findUserRes['errorCode'] == 0) {
            if ($isAdd) {
                $data = [
                    'total_balance' => (float)$px_commission_money + (float)$findUserRes['data']['total_balance'],
                    'balance' => (float)$px_commission_money + (float)$findUserRes['data']['balance'],
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$px_commission_money,
                ];
                $zj_state = 0;
            } else {
                $data = [
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$px_commission_money,
                ];
                $zj_state = 2;
            }
            $adminModel->updateAdmin(['id' => $px_admin_id], $data);
            echo $adminModel->getLastSql()."\r\n";
            $adminBalanceRecordsModel->updateCommissionRecords([
                'user_id' => $px_admin_id,
                'order_id' => $order_id,
                'zj_state' => 1
            ],['zj_state' => $zj_state]);
            echo $adminBalanceRecordsModel->getLastSql()."\r\n";
            echo '----------updatePxAdminCommission-----------'."\r\n";
            $pxbStaffListRes = $adminModel->findAllAdmin(['bid' => $px_admin_id]);
            if ($pxbStaffListRes['errorCode'] == 0) {
                $pxAdminMoney = $px_commission_money;
                array_map(function ($staff) use ($pxAdminMoney,$adminModel,$adminBalanceRecordsModel,$isAdd,$order_id) {
                    if (floatval($staff['rate'])) {
                        $staff_commission_money = $staff['rate'] * $pxAdminMoney;
                        $staff_commission_money = (float)sprintf("%.2f", substr(sprintf("%.3f", $staff_commission_money), 0, -1));
                        if ($isAdd) {
                            $data = [
                                'total_balance' => $staff_commission_money + (float)$staff['total_balance'],
                                'balance' => $staff_commission_money + (float)$staff['balance'],
                                'commission' => (float)$staff['commission'] - $staff_commission_money,
                            ];
                            $zj_state = 0;
                        } else {
                            $data = [
                                'commission' => (float)$staff['commission'] - $staff_commission_money,
                            ];
                            $zj_state = 2;
                        }
                        $adminModel->updateAdmin(['id' => $staff['id']], $data);
                        echo $adminModel->getLastSql()."\r\n";
                        $adminBalanceRecordsModel->updateCommissionRecords([
                            'user_id' => $staff['id'],
                            'order_id' => $order_id,
                            'zj_state' => 1
                        ],['zj_state' => $zj_state]);
                        echo $adminBalanceRecordsModel->getLastSql()."\r\n";
                        echo '----------员工-----------'."\r\n";
                    }
                },$pxbStaffListRes['data']);
            }
        }
    }

    /**
     * Info: 门店用户
     * Date: 2023/1/29 16:39
     */
    public static function updateStoreUserCommission (
        $memberModel,
        $userModel,
        $balanceRecordsModel,
        $store_user_id,
        $order_id,
        $md_commission_money,
        $isAdd = true)
    {
        $findUserRes = $memberModel->findMember(['user_id' => $store_user_id]);
        if ($findUserRes['errorCode'] == 0) {
            if ($isAdd) {
                $data = [
                    'total_balance' => (float)$md_commission_money + (float)$findUserRes['data']['total_balance'],
                    'balance' => (float)$md_commission_money + (float)$findUserRes['data']['balance'],
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$md_commission_money,
                ];
                $zj_state = 0;
            } else {
                $data = [
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$md_commission_money,
                ];
                $zj_state = 2;
            }
            $memberModel->updateMember(['user_id' => $store_user_id], $data);
            echo $memberModel->getLastSql()."\r\n";
            $userModel->updateUser(['id' => $store_user_id], $data);
            echo $userModel->getLastSql()."\r\n";
            $balanceRecordsModel->updateCommissionRecords([
                'user_id' => $store_user_id,
                'order_id' => $order_id,
                'zj_state' => 1
            ],['zj_state' => $zj_state]);
            echo $balanceRecordsModel->getLastSql()."\r\n";
            echo '----------updateStoreUserCommission-----------'."\r\n";
        }
    }

    /**
     * Info: 门店上级用户
     * Date: 2023/1/29 16:40
     */
    public static function updateStoreParentUserCommission (
        $memberModel,
        $userModel,
        $balanceRecordsModel,
        $store_parent_user_id,
        $order_id,
        $store_parent_user_commission_money,
        $isAdd = true)
    {
        $findUserRes = $memberModel->findMember(['user_id' => $store_parent_user_id]);
        if ($findUserRes['errorCode'] == 0) {
            if ($isAdd) {
                $data = [
                    'total_balance' => (float)$store_parent_user_commission_money + (float)$findUserRes['data']['total_balance'],
                    'balance' => (float)$store_parent_user_commission_money + (float)$findUserRes['data']['balance'],
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$store_parent_user_commission_money,
                ];
                $zj_state = 0;
            } else {
                $data = [
                    'commission' => (float)$findUserRes['data']['commission'] - (float)$store_parent_user_commission_money,
                ];
                $zj_state = 2;
            }
            $memberModel->updateMember(['user_id' => $store_parent_user_id], $data);
            echo $memberModel->getLastSql()."\r\n";
            $userModel->updateUser(['id' => $store_parent_user_id], $data);
            echo $userModel->getLastSql()."\r\n";
            $balanceRecordsModel->updateCommissionRecords([
                'user_id' => $store_parent_user_id,
                'order_id' => $order_id,
                'zj_state' => 1
            ],['zj_state' => $zj_state]);
            echo $balanceRecordsModel->getLastSql()."\r\n";
            echo '----------updateStoreParentUserCommission-----------'."\r\n";
        }
    }
}